﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SwenAssignment3.Classes;
using System.Collections;

namespace SwenAssignment3.RoomAvailabilityCumBookingModule
{
    public partial class RoomBooking : System.Web.UI.Page
    {
        int numOfRooms = 0;
        RoomsBooking rb = new RoomsBooking();
        string bookingID = "";//Desired Start & End Date
        Invoice newInvoice = new Invoice(); // need to pass
        string invoiceID = "";
        DateTime now = DateTime.Now;
        string roomType = "";
        string smokingOrNot = "";
        HiddenField roomStatus;
        LinkButton roomNo;
        Guest newGuest; // need to pass
        List<RoomsBooking> bookingList = new List<RoomsBooking>(); // need to pass
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                cblRoomType.DataSource = DRHotelDBManager.retrieveAllRoomType();
                cblRoomType.DataBind();
                UserAcc a = DRHotelDBManager.retrieveUser((string)Session["username"]);
                rb.StaffID = a.StaffID;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //Changing the image of image-buttons
            ImageButton2.ImageUrl = "~/Pictures/NSelectTheRoom.png";
            ImageButton3.ImageUrl = "~/Pictures/NEnterGuestDetail.png";

            //Redirect to View 1
            MultiView1.ActiveViewIndex = 0;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            directToView2();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            directToView2();
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            directToView3();
        }

        protected void btnNext2_Click(object sender, EventArgs e)
        {
            newGuest = new Guest();
            newGuest.GuestID = tbxGuestID.Text;
            newGuest.FirstName = tbxFirstName.Text;
            newGuest.LastName = tbxLastName.Text;
            newGuest.PhoneNo = Convert.ToInt32(tbxContactNo.Text);
            newGuest.Email = tbxEmail.Text;
            newGuest.HomeAddress = tbxHomeAddr.Text;
            newGuest.Nationality = ddlCountry.SelectedValue;

            //putting all haven't input data into session as pass over to next page
            var g = newGuest;
            Session["guest"] = g;
            var inv = newInvoice;
            Session["invoice"] = inv;
            var roomlist = bookingList;
            Session["bookingList"] = roomlist;
            Response.Redirect("PaymentPage.aspx");
        }

        protected void generateInvoiceID()
        {
            invoiceID = generateRandomString();
            if (DRHotelDBManager.searchInvoiceID(invoiceID) == true)
            {
                generateInvoiceID();
            }
            else
            {
                newInvoice.InvoiceID = invoiceID;
            }
        }

        protected void generateBookingID()
        {
            bookingID = generateRandomString();
            if(DRHotelDBManager.searchBookingID(bookingID) == true)
            {
                generateBookingID();
            }
            else
            {
                rb.BookingID = bookingID;
                newInvoice.BookingID = bookingID;
            }
        }

        protected string generateRandomString()
        {
            string ID = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string strid = "";
            Random rnd = new Random();
            for (int i = 0; i <= 3; i++)
            {
                int randomint = rnd.Next(0, ID.Length - 1);
                strid += ID.Substring(randomint, 2);
            }
            return strid;
        }

        protected void directToView2()
        {
            generateBookingID();
            // Number of rooms the guest wanted.
            numOfRooms = Convert.ToInt32(ddlNoOfRooms.SelectedValue);

            // The date the guest is booking the room on..
            rb.DesiredCheckInDate = Convert.ToDateTime(tbxStartDate.Text);
            rb.DesiredCheckOutDate =  Convert.ToDateTime(tbxEndDate.Text);

            // Type of rooms that the guest want to books
            roomType = cblRoomType.SelectedValue;
            
            smokingOrNot = "Y";
            if (rblSmoking.SelectedValue == "No")
            {
                smokingOrNot = "N";
            }

            if (rb.DesiredCheckInDate.Day <= now.Day - 1)
            {
                lblDateStatus.Text = "Check-In date cannot be from the past";
                if(rb.DesiredCheckOutDate.Day <= now.Day - 1)
                {
                    lblDateStatus.Text = "Check-Out date cannot be from the past";
                }
            }
            else
            {
                //Changing the image of image-buttons
                ImageButton2.ImageUrl = "~/Pictures/SelectTheRoom.png";
                ImageButton3.ImageUrl = "~/Pictures/NEnterGuestDetail.png";

                //Direct to View 2
                MultiView1.ActiveViewIndex = 1;

                // Compare the booking DateTime with DateTime.now
                if (DateTime.Compare(rb.DesiredCheckInDate, now) <= 0)
                {
                    // If the customer choose to book a room from the front-desk staff..
                    generateInvoiceID();
                    dlRoomList.DataSource = DRHotelDBManager.retrieveAllRoomsByDetail(roomType, smokingOrNot);
                    dlRoomList.DataBind();
                }
                else
                {
                    // If the customer choose to reserve a room for other dates..
                    dlRoomList.DataSource = DRHotelDBManager.retrieveAllRoomsByDate(rb.DesiredCheckInDate, rb.DesiredCheckOutDate, roomType, smokingOrNot);
                    dlRoomList.DataBind();
                }
            }
        }

        protected void directToView3()
        {
            if (numOfRooms == 0)
            {
                //prompt for redirect

                //Changing the image of image-buttons
                ImageButton3.ImageUrl = "~/Pictures/EnterGuestDetail.png";

                //Redirect to View 3
                MultiView1.ActiveViewIndex = 2;
            }
            else
            {
            
            }
        }

        // Binding the CSS Style of item in the DataList to Database..
        protected void dlRoomList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            roomStatus = (HiddenField)e.Item.FindControl("RoomStatus");
            roomNo = (LinkButton)e.Item.FindControl("RoomButton");

            if (roomStatus.Value == "O")
            {
                roomNo.CssClass = "OccupiedRoom";
            }
            else if (roomStatus.Value == "SFC")
            {
                roomNo.CssClass = "SFCRoom";
            }
            else if (roomStatus.Value == "R")
            {
                roomNo.CssClass = "ReservedRoom";
            }
            else if (roomStatus.Value == "S")
            {
                roomNo.CssClass = "SelectedRoom";
            }
            else
            {
                roomNo.CssClass = "VacantRoom";
            }
        }

        protected void dlRoomList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "RoomButtonClick")
            {
                roomNo = e.Item.FindControl("RoomButton") as LinkButton;
                if (roomNo.CssClass == "VacantRoom")
                {
                    //If Room Status = Available
                    roomNo.CssClass = "SelectedRoom";

                    //Add RoomBooking into a List
                    rb.RoomNo = Convert.ToInt32(roomNo.Text);
                    bookingList.Add(rb);
                    --numOfRooms;
                    if (numOfRooms == 0)
                    {
                        directToView3();
                    }
                }
                else if (roomNo.CssClass == "SelectedRoom")
                {
                    //If Room Status = Selected
                    roomNo.CssClass = "VacantRoom";

                    //Remove item from List
                    foreach (RoomsBooking r in bookingList)
                    {
                        if (r.RoomNo == Convert.ToInt32(roomNo))
                        {
                            bookingList.Remove(r);
                            numOfRooms++;
                            break;
                        }
                    }
                }
                else
                {
                    //For any other room status nothing to occur when its being clicked..
                }
            }
        }
    }
}